import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { GB } from '../../../environments/environment';
import { TranslatePipe, TranslateService } from '@ngx-translate/core';
import { I18nService } from '../../core/i18n/i18n.service';
import { ApiService } from 'src/app/core/api';
import { Global } from 'src/app/core/global';
import { Router } from '@angular/router';
import { StorageService } from 'src/app/core/sotrage/storage.service';
import { RatingConfig } from 'ngx-weui';

@Component({
	selector: 'app-index',
	templateUrl: './index.component.html',
	styleUrls: ['./index.component.less'],
})
export class SearchComponent implements OnInit {
	isShow=false;
	lang = this.sto.get('lang')?this.sto.get('lang'):'zh';
	text='';
	textList=[];
	list =[];
	hotList=[];
	customIconsAndClassCog: RatingConfig = {
		max:5,
		stateOff: 'off',
		stateOn: 'on',
		cls: 'rating'
	}
	constructor(private i: I18nService, private api: ApiService, private global: Global, private router: Router, private sto: StorageService,  private translate: TranslateService) {
	}
	pageData = {
		pageNo: "1",
		pages: "1",
		genre01Id: '',
		genre02Id: '',
		gameName: '',
		providerId:''
	};
	ngOnInit(): void {
		this.getText();
		this.getHot();
		this.getList();
	}
	onClose(){
		this.isShow=false;
		this.text='';
	}
	search(){
		this.getList();
	}
	searchClear(){
		this.pageData.gameName='';
		this.getList();
	}
	textSearch(text){
		this.pageData.gameName = text ? text : '';
		this.getList()	;
	}
	onSel(tegId) {
		this.api.post('开始游戏', {id:tegId}).then(res => {
			this.router.navigate(['/gz'], {
				queryParams: res['data']
			});
		})
	}

	getList() {
		this.api.post('游戏列表', this.pageData).then(res => {
			this.pageData.pageNo = res['data']['pageNo'];
			this.pageData.pages = res['data']['pages'];
			let lists = res['data']['list'];
			if(res['data']['pageNo'] == 1){
				this.list = lists;
			}else{
				for (let i = 0; i < lists.length; i++) {
					this.list.push(lists[i]);
				}
			}
		}).catch(res => {
			return res;
		});
	}
	
	getHot(){
		this.api.post('热门游戏区', this.pageData ).then(res => {
			this.hotList = res['data']['list'];
		}).catch(res => {
			return res;
		});
	}
	getText(){
		this.api.post('搜索热词', {}).then(res => {
			this.textList = res['data']['list'];
		}).catch(res => {
			return res;
		});
	}
}
